


package jnpf.fico.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.fico.entity.ZcglCwZfjlEntity;
import jnpf.fico.model.zfjl.ZcglCwZfjlForm;
import jnpf.fico.model.zfjl.ZcglCwZfjlInfoVO;
import jnpf.fico.model.zfjl.ZcglCwZfjlListVO;
import jnpf.fico.model.zfjl.ZcglCwZfjlPagination;
import jnpf.fico.service.ZcglCwZfjlService;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * 支付记录
 *
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-10-31
 */
@Slf4j
@RestController
@Api(tags = "支付记录", value = "example")
@RequestMapping("/api/example/ZcglCwZfjl")
public class ZcglCwZfjlController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglCwZfjlService zcglCwZfjlService;


    /**
     * 支付记录列表
     * @param zcglCwZfjlPagination
     * @return
     */
    @ApiOperation("获取支付记录列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglCwZfjlPagination zcglCwZfjlPagination) throws IOException {
        List<ZcglCwZfjlEntity> list = zcglCwZfjlService.getList(zcglCwZfjlPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<ZcglCwZfjlListVO> listVO = JsonUtil.getJsonToList(list, ZcglCwZfjlListVO.class);
        for (ZcglCwZfjlListVO zcglCwZfjlVO : listVO) {
            zcglCwZfjlVO.setBillNo_name(zcglCwZfjlVO.getBillNo());

            zcglCwZfjlVO.setPaymentCode_name(zcglCwZfjlVO.getPaymentCode());

            zcglCwZfjlVO.setPaymentType_name(zcglCwZfjlVO.getPaymentType());

            zcglCwZfjlVO.setPaymentAccount_name(zcglCwZfjlVO.getPaymentAccount());

            zcglCwZfjlVO.setPaymentTime_name(zcglCwZfjlVO.getPaymentTime());

            zcglCwZfjlVO.setPaymentAmount_name(zcglCwZfjlVO.getPaymentAmount());

            zcglCwZfjlVO.setLateFee_name(zcglCwZfjlVO.getLateFee());

            zcglCwZfjlVO.setPaymentUser_name(zcglCwZfjlVO.getPaymentUser());

            zcglCwZfjlVO.setStatus_name(zcglCwZfjlVO.getStatus());

            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcglCwZfjlPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param zcglCwZfjlForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglCwZfjlForm zcglCwZfjlForm) throws DataException {
        boolean b = zcglCwZfjlService.checkForm(zcglCwZfjlForm, 0);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglCwZfjlEntity entity = JsonUtil.getJsonToBean(zcglCwZfjlForm, ZcglCwZfjlEntity.class);
        entity.setId(mainId);
        zcglCwZfjlService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglCwZfjlInfoVO> info(@PathVariable("id") String id) {
        ZcglCwZfjlEntity entity = zcglCwZfjlService.getInfo(id);
        ZcglCwZfjlInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglCwZfjlInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglCwZfjlInfoVO> detailInfo(@PathVariable("id") String id) {
        ZcglCwZfjlEntity entity = zcglCwZfjlService.getInfo(id);
        ZcglCwZfjlInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglCwZfjlInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setBillNo(vo.getBillNo());

        vo.setPaymentCode(vo.getPaymentCode());

        vo.setPaymentType(vo.getPaymentType());

        vo.setPaymentAccount(vo.getPaymentAccount());

        vo.setPaymentTime(vo.getPaymentTime());

        vo.setPaymentAmount(vo.getPaymentAmount());

        vo.setLateFee(vo.getLateFee());

        vo.setPaymentUser(vo.getPaymentUser());

        vo.setStatus(vo.getStatus());


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param zcglCwZfjlForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZcglCwZfjlForm zcglCwZfjlForm) throws DataException {
        boolean b = zcglCwZfjlService.checkForm(zcglCwZfjlForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglCwZfjlEntity entity = zcglCwZfjlService.getInfo(id);
        if (entity != null) {
            ZcglCwZfjlEntity subentity = JsonUtil.getJsonToBean(zcglCwZfjlForm, ZcglCwZfjlEntity.class);
            boolean b1 = zcglCwZfjlService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcglCwZfjlEntity entity = zcglCwZfjlService.getInfo(id);
        if (entity != null) {
            zcglCwZfjlService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


}
